﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using CloudTaobao.Util;

namespace CloudTaobao
{
    public partial class FindPage : PhoneApplicationPage
    {

        Find fd = new Find();
        private string query = "";
        private bool isHideSearchAnimating = false;
        private bool isShowSearchAnimating = false;
        private bool isSearchHidden = false;
        

        public FindPage()
        {
            InitializeComponent();
            fd.startLoadingEvent += new StartLoadingEventHandler(fd_startLoadingEvent);
            fd.endLoadingEvent += new EndLoadingEventHandler(fd_endLoadingEvent);
            fd.itemList.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(itemList_CollectionChanged);

            DataContext = fd;
            UserList.ItemsSource = fd.userList;
            ItemList.ItemsSource = fd.itemList;
        }

        void itemList_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
        {
            if (ItemList.Items.Count == 0 || ItemList.Items.Count == 1)
                return;
            ItemList.ScrollIntoView(ItemList.Items[0]);
            ItemList.UpdateLayout();
        }

        void fd_endLoadingEvent()
        {
            LoadingBar.IsIndeterminate = false;
            LoadingBar.Visibility = Visibility.Collapsed;
        }

        void fd_startLoadingEvent(object source, StartLoadingEventArgs args)
        {
            LoadingBar.IsIndeterminate = true;
            LoadingBar.Visibility = Visibility.Visible;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (searchbox.Text.Equals("")) 
            {
                return;
            }
            query = searchbox.Text;

            

            try
            {
                if (!isHideSearchAnimating&&!isSearchHidden)
                {
                    ShowSearchButton.Visibility = Visibility.Visible;
                    Storyboard sb = new Storyboard();
                    DoubleAnimation ta = Utils.getTransparentAnimation(1, 0, 0.5, SearchStack);
                    ta.Completed += new EventHandler(ta_Completed);
                    sb.Children.Add(ta);
                    sb.Children.Add(Utils.getTransparentAnimation(0, 1, 0.5, ShowSearchButton));
                    sb.Children.Add(Utils.getMarginChangeAnimation(new Thickness(-12, 36, -12, 0), new Thickness(-12, -10, -12, 0), new Duration(new TimeSpan(0, 0, 0, 1)), ShowSearchButton));
                    sb.Children.Add(Utils.getMarginChangeAnimation(new Thickness(0, 78, 0, 0), new Thickness(0, 22, 0, 0), new Duration(new TimeSpan(5000000)), pivotContrl));
                    sb.Children.Add(Utils.getMarginChangeAnimation(new Thickness(0, 78, 0, 0), new Thickness(0, 22, 0, 0), new Duration(new TimeSpan(5000000)), LoadingBar));
                    sb.Begin();
                    isHideSearchAnimating = true;
                }

                AppSettings settings = new AppSettings();
                settings.query = query;

                switch (pivotContrl.SelectedIndex)
                {
                    case 0:
                        fd.totalItems = 0;
                        fd.currentItemPage = 1;
                        fd.GetItemList(query, fd.currentItemPage, Utils.getRecordsNum(), Utils.isShowPic());
                        LoadingBar.Visibility = Visibility.Visible;
                        break;
                    case 1:
                        fd.GetUserInfo(query);
                        break;
                    case 2:
                        //fd.totalShops = 0;
                        //fd.currentShopPage = 1;
                        //fd.GetUserInfo(query);
                        break;
                    default:
                        return;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        void ta_Completed(object sender, EventArgs e)
        {
            isHideSearchAnimating = false;
            isSearchHidden = true;
        }

        private void pivotContrl_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
        {
        	// TODO: Add event handler implementation here.
            if (pivotContrl.SelectedIndex == 0)
                ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["itemBar"];
            else if (pivotContrl.SelectedIndex == 1)
                ApplicationBar = (Microsoft.Phone.Shell.ApplicationBar)Resources["infoBar"];
        }

        private void myInfo_Click(object sender, EventArgs e)
        {
            try
            {
                App app = Application.Current as App;
                if (!app.sessionInfo.ContainsKey("top_session") || app.sessionInfo["top_session"] == "")
                    return;
                string nick = app.sessionInfo["nick"];
                if (nick != null)
                {
                    fd.GetUserInfo(nick);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void previousItemPage_Click(object sender, EventArgs e)
        {
            try
            {
                if (fd.currentItemPage > 1 && query != "")
                {
                    fd.GetItemList(query, --fd.currentItemPage, Utils.getRecordsNum(), Utils.isShowPic());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void nextItemPage_Click(object sender, EventArgs e)
        {
            try
            {
                if ((fd.currentItemPage * Utils.getRecordsNum() < fd.totalItems) && query != "")
                {
                    fd.GetItemList(query, ++fd.currentItemPage, Utils.getRecordsNum(), Utils.isShowPic());
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void filters_Click(object sender, EventArgs e)
        {
            NavigationService.Navigate(new Uri("/FilterSettings.xaml", UriKind.Relative));
        }

        private void itemList_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (ItemList.SelectedItem == null)
            {
                return;
            }
            App app = Application.Current as App;
            app.tmpItem = (Item)ItemList.SelectedItem;
            NavigationService.Navigate(new Uri("/ItemPage.xaml", UriKind.Relative));
        }

        private void searchbox_GotFocus(object sender, RoutedEventArgs e)
        {
            InputBkg.Visibility = Visibility.Visible;
        }

        private void searchbox_LostFocus(object sender, RoutedEventArgs e)
        {
            InputBkg.Visibility = Visibility.Collapsed;
        }

        private void ShowSearchButton_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (!isShowSearchAnimating && isSearchHidden)
                {
                    Storyboard sb = new Storyboard();
                    DoubleAnimation ta_show = Utils.getTransparentAnimation(0, 1, 0.3, SearchStack);
                    ta_show.Completed += new EventHandler(ta_show_Completed);
                    sb.Children.Add(ta_show);
                    sb.Children.Add(Utils.getTransparentAnimation(1, 0, 0.3, ShowSearchButton));
                    sb.Children.Add(Utils.getMarginChangeAnimation(new Thickness(-12, -10, -12, 0), new Thickness(-12, 36, -12, 0), new Duration(new TimeSpan(0, 0, 0, 1)), ShowSearchButton));
                    sb.Children.Add(Utils.getMarginChangeAnimation(new Thickness(0, 22, 0, 0), new Thickness(0, 78, 0, 0), new Duration(new TimeSpan(3000000)), pivotContrl));
                    sb.Children.Add(Utils.getMarginChangeAnimation(new Thickness(0, 22, 0, 0), new Thickness(0, 78, 0, 0), new Duration(new TimeSpan(3000000)), LoadingBar));
                    sb.Begin();
                    isShowSearchAnimating = true;
                }
            }
            catch (Exception)
            {
                return;
            }
        }

        void ta_show_Completed(object sender, EventArgs e)
        {
            isShowSearchAnimating = false;
            isSearchHidden = false;
            ShowSearchButton.Visibility = Visibility.Collapsed;
            searchbox.Focus();
        }
        /*
        private void Grid_MouseEnter(object sender, MouseEventArgs e)
        {
            try
            {
                ((Grid)sender).Background = new SolidColorBrush(Color.FromArgb(255, 240, 240, 240));
            }
            catch (Exception)
            {
                return;
            }
        }

        private void Grid_MouseLeave(object sender, MouseEventArgs e)
        {
            try
            {
                ((Grid)sender).Background = new SolidColorBrush(Color.FromArgb(255, 255, 255, 255));
            }
            catch (Exception)
            {
                return;
            }
        }*/
    }
}